package com.asktgapp.user.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.modulebase.common.util.Util;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/1/8
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:重置密码，根据原密码设置新密码
 */

public class RevisePwdFragment extends BaseFragment {
    @InjectView(R.id.iv_eye)
    ImageView mEye;
    @InjectView(R.id.iv_eye1)
    ImageView mEye1;
    @InjectView(R.id.iv_eye2)
    ImageView mEye2;
    @InjectView(R.id.et_pwd)
    EditText mPwdET;
    @InjectView(R.id.et_pwd1)
    EditText mPwdET1;
    @InjectView(R.id.et_pwd2)
    EditText mPwdET2;
    @InjectView(R.id.tv_commit)
    TextView mCommit;

    private boolean isHide = true;
    private boolean isHide1 = true;
    private boolean isHide2 = true;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_revise_password, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        mEye2.setOnClickListener(this);
        mEye.setOnClickListener(this);
        mEye1.setOnClickListener(this);
        mCommit.setOnClickListener(this);

    }

    @Override
    public void loadData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_eye:
                String pwd = mPwdET.getText().toString().trim();
                if (!Util.isEmpty(pwd)) {
                    if (isHide) {
                        mPwdET.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                        mEye.setImageResource(R.mipmap.ico_zhengyan);
                    } else {
                        mPwdET.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                        mEye.setImageResource(R.mipmap.ico_biiyan);
                    }
                    mPwdET.setSelection(pwd.length());
                    isHide = !isHide;
                }

                break;

            case R.id.iv_eye1:

                String pwd1 = mPwdET1.getText().toString().trim();
                if (!Util.isEmpty(pwd1)) {
                    if (isHide1) {
                        mPwdET1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                        mEye1.setImageResource(R.mipmap.ico_zhengyan);
                    } else {
                        mPwdET1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                        mEye1.setImageResource(R.mipmap.ico_biiyan);
                    }
                    mPwdET1.setSelection(pwd1.length());
                    isHide1 = !isHide1;
                }
                break;
            case R.id.iv_eye2:
                String pwd2 = mPwdET2.getText().toString().trim();
                if (!Util.isEmpty(pwd2)) {
                    if (isHide2) {
                        mPwdET2.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                        mEye2.setImageResource(R.mipmap.ico_zhengyan);

                    } else {
                        mPwdET2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                        mEye2.setImageResource(R.mipmap.ico_biiyan);
                    }
                    mPwdET2.setSelection(pwd2.length());
                    isHide2 = !isHide2;
                }
                break;
            case R.id.tv_commit:
                commit();
                break;
        }
    }


    /**
     * 提交
     */
    private void commit() {
        String passWordOld = mPwdET.getText().toString().trim();
        String passWordNew = mPwdET1.getText().toString().trim();
        String passWordNewSecond = mPwdET2.getText().toString().trim();
        if (Util.isEmpty(passWordOld)) {
            showTost("请输入旧密码", 0);
            return;
        }
        if (Util.isEmpty(passWordNew)) {
            showTost("请输入新密码", 0);
            return;
        }
        if (Util.isEmpty(passWordNewSecond)) {
            showTost("请确认新密码", 0);
            return;
        }
        if (!passWordNew.equals(passWordNewSecond)) {
            showTost("两次输入的密码不一致", 1);
            return;
        }
//        String oldpwd = PreferencesUtil.getString(getActivity(), PreferencesUtil.KEY_PASSWORD);
//        if (!oldpwd.equals(passWordOld)) {
//            showTost("原密码错误，请重新输入", 1);
//            return;
//        }
        final Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("newPwd", Util.encodeMD5(passWordNew));
        params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
        params.put("type", 0);
        params.put("oldPwd", Util.encodeMD5(passWordOld));
        showProgress(getClass().getSimpleName());
        Call<ApiResponseBody> call = apiservice.updateNick(params);
        call.enqueue(new Callback<ApiResponseBody>() {
            @Override
            public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                dismissProgress();
                if (response.isSuccessful()) {
                    showTost("密码修改成功", 1);
                    getActivity().finish();
                } else {
                    showTost(response.raw().message(), 1);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                dismissProgress();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 1);
                }
            }
        });
    }
}
